<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 1/19/15
 * Time: 11:43 AM
 */

namespace DinhThi\MVC;


class AbstractController {
    protected $view;
    public function __construct(){
        $this->view = View::getInstance();
    }

    /**
     * Redirect to $controller class, $action function
     * @param $controller
     * @param string $action
     * @param string $subpath
     * @param null $params
     */
    public function redirect($controller = '', $action = '', $params = null, $subpath =''){
        $config = Configuration::getInstance();
        $parameters = array();

        if ($action != '')
            $action .= '/';

        if ($controller != '')
            $controller .= '/';

        foreach ($params as $key => $value){
            $parameters[] = $key . '=' . $value;
        }
        $parameters = implode('&', $parameters);
        if ($params != null){
            header('Location: ' . $config->getProperty('BASEURL') . $controller . $action . $subpath. '?' . $parameters);
        }
        else
            header('Location: ' . $config->getProperty('BASEURL') . $controller . $action . $subpath);
    }
} 